package com.newtouch.cq.security.util;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.newtouch.cq.lang.em.BaseExceptionEnum;
import com.newtouch.cq.lang.util.LogUtil;
import com.newtouch.cq.security.exception.AuthException;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Alex
 * @Description MD5密码加密
 * @date 2020-12-09 14:35:00
 */
public class CustomwordEncoder implements PasswordEncoder {

    private Log log = LogFactory.get(CustomwordEncoder.class);

    @Override
    public String encode(CharSequence charSequence) {
        return PWDUtil.encryption((String) charSequence);
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        if(!PWDUtil.check((String) charSequence,s)) {
            LogUtil.error(log,BaseExceptionEnum.ACCOUNT_PASSWORD_ERROR);
            throw new AuthException(BaseExceptionEnum.ACCOUNT_PASSWORD_ERROR);
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(DigestUtil.md5Hex("123456"));
    }
}
